/**
 * global.js - JavaScript Document
 *
 * NMIT BIT Project - Nova - A 3D Website using WebGL & HTML5 Technology
 * Infinity Grid Version 
 * Version: 0.1.2
 *
 * Author: William Liu <qzliuyuzou@gmail.com> <lyzdev@gmail.com>
 *
 * Required Server Version:
 *  PHP5 or higher
 *  MySQL 5 or higher
 *
 * Required Browser Version:
 *  Google Chrome 9 or higher
 *  Mozilla Firefox 4 Beta or higher
 *  *Apple Safari
 *
 * Copyright (c) 2011, William Liu All rights reserved.
 * Infinity is an open source 3D website project
 * Under Apache License 2.0
 * http://www.lyzsoft.cn
 * http://7cloud.wordpress.com
 *
 * Project Starting date: 9/3/2011
 * Update Date 1: 19/3/2011
 * Uptade Date 2: 5/5/2011
 * Update Date 3: 6/5/2011
 * Update Date 4: 5/6/2011 - Beta 2
 *
**/

$(function(){
	$('#comment').bind("blur focus keydown keypress keyup", function(){
		counter();
	});
    $("#txtarea").submit(function(){
		//var submitData = $(this).serialize();
		var comment = $("#comment").val();
		if(comment==""){
			$("#warning").show().html("Please say something...").fadeOut(3600);
			return false;
		}
		//$('.counter').html('<img style="padding:56px 21px" src="images/load.gif" alt="Loading..." />');
		$('.counter').html('<img src="images/load.gif" alt="Loading..." />');
		
		
		$.ajax({
		   type: "POST",
		   url: "submit.php",
		   //data: submitData,
		   data:"comment="+comment,
		   dataType: "html",
		   success: function(msg){
			  if(parseInt(msg)!=0){
				 $('#msgwrap').prepend(msg);
				 $('#comment').val('');
				 counter();
			 }
		  }
	    });
		return false;
	});
});

function counter(){
	var maxlen=140;
	var current = maxlen-$('#comment').val().length;
	$('.counter').html(current);

	if(current<1 || current>maxlen){
		$('.counter').css('color','#D40D12');
		$('input.push').attr('disabled','disabled');
	}
	else
		$('input.push').removeAttr('disabled');

	if(current<10)
		$('.counter').css('color','#D40D12');

	else if(current<20)
		$('.counter').css('color','#FFE51E');

	else
		$('.counter').css('color','#A1FF66');
}

//ctrl + enter key combination;
//use for submit msg
document.addEventListener('keyup', ctrlEnter, false);
function ctrlEnter(event){
	
	//var e=event.srcElement; 
	if(event.ctrlKey && event.keyCode == 13) 
	{ 
	  document.getElementById("submit").click(); 
	  return false; 
	} 
}
